import { Controller, Get, Post, Body, Patch, Param, Delete, Req } from '@nestjs/common';

import { error, success } from '../../utils/index';

import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserService } from './user.service';

@Controller('user')
export class UserController {
    constructor(private readonly userService: UserService) {}

    // getUserInfoByToken
    @Get('info')
    getUserInfoByToken(@Req() request: any) {
        return this.userService
            .findByUsername(request.user.username)
            .then((data) => success(data, '获取用户信息成功'))
            .catch((err) => error(err.message));
    }

    // 增删改查
    @Post()
    create(@Body() createUserDto: CreateUserDto) {
        console.log('ctrl', createUserDto);

        return this.userService.create(createUserDto);
    }

    @Get()
    findAll() {
        return this.userService.findAll();
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.userService.findOne(+id);
    }

    @Patch(':id')
    update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
        return this.userService.update(+id, updateUserDto);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.userService.remove(+id);
    }
}
